package org.example;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.Clipboard;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.awt.*;


/**
 * 实现
 *
 * */
public class Main extends Application {
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Clipboard clipboard = Clipboard.getSystemClipboard();
    String isval ="";
    String box = "";
    String doStr="";
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Label labelinfomages = new Label("LOADING...");
        labelinfomages.setPadding(new Insets(0,0,0,20));
        labelinfomages.setAlignment(Pos.CENTER_LEFT);
        labelinfomages.setStyle("-fx-text-fill: #ffffff;-fx-font-size: 15;-fx-font-weight: bold");
        labelinfomages.setPrefWidth(380);
        labelinfomages.setPrefHeight(50);
        Pane root = new Pane();
        root.setStyle("-fx-background-color: #000000;-fx-font-family: 'Courier New'");
        Scene scene = new Scene(root,380,50);
        root.getChildren().addAll(labelinfomages);
        primaryStage.setTitle("Snail 权限词检测工具");
        primaryStage.setScene(scene);
        primaryStage.resizableProperty().setValue(Boolean.FALSE);
        primaryStage.setResizable(false);
        primaryStage.initStyle(StageStyle.values()[3]);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.setX(screenSize.getWidth()-600);
        primaryStage.setY(30);
        clipboard.clear();
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent event) {
                primaryStage.close();
                System.exit(0);
            }
        });
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    synchronized (this){
                        primaryStage.setX(screenSize.getWidth()-600);
                        primaryStage.setY(30);
                    }
                }
            }
        });
        thread.start();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Thread threadText = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true){
                            synchronized (this){
                                try {
                                    Thread.sleep(500);
                                    Platform.runLater(()->{
                                        isval = clipboard.getString();
                                    });
//                            极佳
                                    if(box.equals(isval)==false && isval.equals(null)==false && isval.equals("")==false){
                                        box= isval;
                                        Connection con = Jsoup.connect("http://www.mgzxzs.com/sytool/chajixianci/ci_post.php").postDataCharset("UTF-8");
                                        con.method(Connection.Method.POST);
                                        con.timeout(200000);
                                        con.data("data",box);
                                        con.data("gjif","zhchaci");
                                        Document document = con.post();
                                        Elements title = document.body().select("font");
                                        String doStr =title.text();
                                        System.out.println(doStr);
                                        if(doStr.indexOf("恭喜")<0){
                                            Platform.runLater(()->{
                                                labelinfomages.setStyle("-fx-text-fill: #FF0000;-fx-font-size: 15;-fx-font-weight: bold");
                                                labelinfomages.setText("ERROR: "+doStr);
                                                clipboard.clear();
                                            });
                                        }else {
                                            Platform.runLater(()->{
                                                labelinfomages.setText("SUCCESS: OK");
                                                labelinfomages.setStyle("-fx-text-fill: rgb(0,255,0);-fx-font-size: 15;-fx-font-weight: bold");
//                                                clipboard.clear();
                                            });
                                        }
                                        isval="";
                                        continue;
                                    }else{
                                        continue;
                                    }
                                } catch (Exception e) {
                                    continue;
                                }
                            }
                        }
                    }
                });
                threadText.start();
            }
        });
        primaryStage.show();
    }
}
